package com.baijiayun.liveuibase.widgets;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;
import com.baijiayun.liveuibase.widgets.SwitchCompat.MultiSwitchCompat;

/**
 * @author wubanglin
 */
public class SettingSwitch extends MultiSwitchCompat {

    private Drawable trackDrawable;

    public SettingSwitch(@NonNull Context context) {
        super(context);
        initDrawable();
    }

    public SettingSwitch(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initDrawable();
    }

    public SettingSwitch(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDrawable();

    }

    private void initDrawable() {
        setThumbDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bjy_base_shape_common_switch_thumb));
        trackDrawable = new DrawableBuilder().solidColor(getContext().getResources().getColor(R.color.bjy_base_setting_switch_background)).build();
        setTrackDrawable(trackDrawable);
        setShowText(true);
        setTextColors(ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_dialog_positive_text_color),
                ThemeDataUtil.getColorFromThemeConfigByAttrId(getContext(), R.attr.base_theme_window_main_text_color));
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        trackDrawable.setAlpha(enabled ? 0xff : 0x80);
        getThumbDrawable().setAlpha(enabled ? 0xff : 0x80);
    }
}
